// pages/cart/cart.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasMore: true,
    page: 1,
    dataList: [],
    default: [
      {id:0,num:1,checked:false},
      {id:1,num:1,checked:false},
      {id:2,num:1,checked:false},
      {id:3,num:1,checked:false},
      {id:4,num:1,checked:false},
      {id:5,num:1,checked:false},
      {id:6,num:1,checked:false},
      {id:7,num:1,checked:false},
      {id:8,num:1,checked:false},
      {id:9,num:1,checked:false}
    ],
    editStatus: false,
    selectedList: [],
    selectAll: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '购物车',
    })

    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getList()
  },

  // 获取列表
  getList () {
    let that = this;
    if (!that.data.hasMore) {
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
    let tData = that.data
    let param = {
      PageIndex: tData.page,
    }
    console.log(param)
    // ajax.ajax(api.cartList, param)
    //   .then((val) => {
    //     console.log(val)
    //     if ((val.Tag == 1) && val.Result) {
    //       let data = tData.dataList.concat(val.Result);
    //       let hasMore = (data.length < val.TotalCount);
    //       let page = tData.page + 1 ;
    //       that.setData({
    //         dataList: data,
    //         hasMore,
    //         page
    //       });
    //       wx.hideLoading();
    //     } else {
    //       wx.showToast({title: "加载失败，请重试", icon: 'none'})
    //     }
    //   })
    //   .catch((val) => {
    //     wx.showToast({title: "加载失败，请重试", icon: 'none'})
    //   })

    setTimeout(() => {
      wx.hideLoading()
      let data = tData.dataList.concat(tData.default);;
      let page = tData.page + 1 ;
      that.setData({
        dataList: data,
        hasMore: true,
        page
      });
    }, 1500)
    
  },

  // 点击减少数量
  handleNumMinus(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let dataList = that.data.dataList;
    let num = dataList[index].num;
    if (num <= 1) { return; }
    num -= 1;
    let key = `dataList[${index}].num`;
    that.setData({
      [key]: num
    })
  },

  // 点击增加数量
  handleNumPlus(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let dataList = that.data.dataList;
    let num = dataList[index].num;
    num += 1;
    let key = `dataList[${index}].num`;
    that.setData({
      [key]: num
    })
  },

  // 编辑状态与否
  changeEditStatus(e) {
    let flag = e.currentTarget.dataset.flag
    let status;
    if (flag == "true") {
      status = true;
    } else if (flag == "false") {
      status = false;
    }

    this.setData({
      editStatus: status
    })
  },

  // 勾选或取消勾选某个商品
  checkboxChange(e) {
    this.setData({
      selectedList: e.detail.value
    })
  },

  // 全选或取消全选
  chooseAll(e) {
    if (e.detail.value.length) {
      this.setListSelectedStatus(true);
    } else {
      this.setListSelectedStatus(false);
    }
  },

  // 改变全列表勾选状态（全选或全不选）
  setListSelectedStatus(flag){
    let that = this;
    let dataList = that.data.dataList;
    dataList = dataList.map(item => {
      item.checked = flag;
      return item;
    })

    that.setData({
      dataList,
      selectAll: flag
    })
  },

  // 移除购物车商品
  confirmDelete() {
    let that = this; 
    if (that.data.selectAll) { // 已加载的商品，全删
      let list = that.data.dataList;
      console.log(list);
    } else { // 删除已勾选的
      let list = that.data.selectedList;
      console.log(list);
    }

    // 筛选出勾选的ID列表

    // 请求接口，成功后刷新页面
  }
})